package cycling.taxi.com.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cycling.taxi.com.R;
import cycling.taxi.com.bean.NoticeInfo;
import cycling.taxi.com.bean.ShopInfo;
import cycling.taxi.com.entity.BusinessNotice;
import cycling.taxi.com.entity.Repair;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.ui.fragment.TipsFrgt;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.Urls;

/**
 * 消息中心
 */
public class MessageListActivity extends Activity implements View.OnClickListener {
    private ImageView back;
    private TextView title_text;
    private ListView usermessageone;
    private ListView usermessagetwo;
    private RequestQueue queue;
    private ProgressBar pb;
    private List<Repair> data;
    private List<BusinessNotice> businessNotice;
    private ShopInfoAdapter adapter;
    private NoticeInfoAdapter noticeInfoAdapter;
    private RadioButton rdb_carMessage;
    private RadioButton rdb_sysmsg_reming;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_message_list);
        ExitApplication.getInstance().addActivity(this);
        InitView();
        usermessageone=(ListView)findViewById(R.id.usermessageone);
        usermessagetwo=(ListView)findViewById(R.id.usermessagetwo);
        pb=(ProgressBar) findViewById(R.id.pb);
        rdb_carMessage=(RadioButton)findViewById(R.id.rdb_carMessage) ;
        rdb_sysmsg_reming=(RadioButton)findViewById(R.id.rdb_sysmsg_reming);
        rdb_carMessage.setOnClickListener(this);
        rdb_sysmsg_reming.setOnClickListener(this);
        adapter=new ShopInfoAdapter();
        noticeInfoAdapter=new NoticeInfoAdapter();
        pb.setVisibility(View.VISIBLE);
        queue= Volley.newRequestQueue(this);
        volleyPost();
        volleyPost2();


    }

    private void InitView() {
        back=(ImageView)findViewById(R.id.back);
        title_text=(TextView)findViewById(R.id.title_text);
        back.setOnClickListener(this);
        title_text.setText("消息中心");


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.rdb_carMessage:
                usermessageone.setVisibility(View.VISIBLE);
                usermessagetwo.setVisibility(View.GONE);
                rdb_carMessage.setChecked(true);
                rdb_sysmsg_reming.setChecked(false);
              //  volleyPost();
                break;
            case R.id.rdb_sysmsg_reming:
                usermessageone.setVisibility(View.GONE);
                usermessagetwo.setVisibility(View.VISIBLE);
                rdb_carMessage.setChecked(false);
                rdb_sysmsg_reming.setChecked(true);
               // volleyPost2();
                break;
        }

    }
    public void volleyPost(){
       // String path= "http://192.168.1.100:8080/CyclingBgTV1/appNotice/notice.dop";
        String path = Urls.getNotice();
        StringRequest request = new StringRequest(Request.Method.POST,path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                data = new Gson().fromJson(s,new TypeToken<List<Repair>>(){}.getType());
                LogUtil.e(s);
                pb.setVisibility(View.GONE);
                usermessageone.setVisibility(View.VISIBLE);
                usermessageone.setAdapter(adapter);

            }
        },null){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String > map= new HashMap<String,String>();
                String beanJson=SPUtil.getInstance().getData("beanJson").replace(" ","%20");
             //   String phone = SPUtil.getInstance().getData("phone");
                map.put("beanJson",beanJson);
             //   map.put("phone",phone);

                return map;
            }
        };
        queue.add(request);
    }
    public void volleyPost2(){
        // String path= "http://192.168.1.100:8080/CyclingBgTV1/appNotice/notice.dop";
        String path = Urls.getBusinessNotice();
        StringRequest request = new StringRequest(Request.Method.POST,path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                businessNotice = new Gson().fromJson(s,new TypeToken<List<BusinessNotice>>(){}.getType());
                pb.setVisibility(View.GONE);
                usermessagetwo.setAdapter(noticeInfoAdapter);

            }
        },null){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String > map= new HashMap<String,String>();
                String beanJson=SPUtil.getInstance().getData("beanJson");
                //   String phone = SPUtil.getInstance().getData("phone");
                map.put("beanJson",beanJson);
                //   map.put("phone",phone);

                return map;
            }
        };
        queue.add(request);
    }
    public static String getStringDate(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }
    class ShopInfoAdapter extends BaseAdapter {

        public ShopInfoAdapter(){

        }
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=View.inflate(MessageListActivity.this,R.layout.activity_notice_item,null);

            }
            Repair repair = data.get(position);
            TextView nameTV =(TextView)convertView.findViewById(R.id.tv_item_name);
            TextView priceTV = (TextView)convertView.findViewById(R.id.tv_item_price);
            TextView tv_item_status=(TextView)convertView.findViewById(R.id.tv_item_status);
            TextView tv_item_createtime=(TextView)convertView.findViewById(R.id.tv_item_createtime);
            TextView tv_item_finshtime=(TextView)convertView.findViewById(R.id.tv_item_finshtime);
            TextView tv_item_dispatch=(TextView)convertView.findViewById(R.id.tv_item_dispatch);
            TextView tv_item_problem=(TextView)convertView.findViewById(R.id.tv_item_problem);
            if(repair.getUserTel()!=null) {
                nameTV.setText(repair.getUserTel());
            }
            if(repair.getImie()!=null){
                priceTV.setText(repair.getImie());
            }
            if(repair.getState()!=null) {
                if(repair.getState().equals("0")) {
                    tv_item_status.setText("未完成");
                }else {
                    tv_item_status.setText("已完成");
                }
            }
            if(repair.getFinishTime()!=null) {
                tv_item_createtime.setText(getStringDate(repair.getCreateTime()));
            }
            if(repair.getFinishTime()!=null){
                tv_item_finshtime.setText(getStringDate(repair.getFinishTime()));
            }
            if(repair.getDispatchAddress()!=null) {
                tv_item_dispatch.setText(repair.getDispatchAddress());
            }
            if(repair.getProblemDescription()!=null) {
                tv_item_problem.setText(repair.getProblemDescription());
            }
            return convertView;
        }
    }
    class NoticeInfoAdapter extends BaseAdapter {

        public NoticeInfoAdapter(){

        }
        @Override
        public int getCount() {
            return businessNotice.size();
        }

        @Override
        public Object getItem(int position) {
            return businessNotice.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=View.inflate(MessageListActivity.this,R.layout.activity_notice_item,null);
                BusinessNotice notice=businessNotice.get(position);
                TextView nameTV =(TextView)convertView.findViewById(R.id.tv_item_name);
                nameTV.setText(notice.getContent());

            }
            return convertView;
        }
    }
}
